import {watch} from "vue";
import {usePlatformUploadStore} from "stores/platform-upload";
import {usePlatformReUploadStore} from "stores/platform-reupload";
import {usePlatformFeatureSetUploadStore} from "stores/platform-featureset-upload";


const platformUploadStore = usePlatformUploadStore();
const platformFeatureSetUploadStore = usePlatformFeatureSetUploadStore()

export const useScheduler = (type: 'dataset' | 'featureSet') => {
  const store =  type == 'dataset' ? platformUploadStore : platformFeatureSetUploadStore
  watch(() => store.platformUploadCount, (count) => {
    if (count < store.platformMaxUploadRequestNum) {
      store.platformPermitScheduler = !store.platformPermitScheduler
    }
  })

  watch(() => [store.platformRequestSignal, store.platformRequestChunkSignal], () => {
    if (store.platformUploadCount < store.platformMaxUploadRequestNum) {
      store.platformPermitScheduler = !store.platformPermitScheduler
    }
  })

  watch(() => store.platformPermitScheduler, () => {
    store.startScheduler()
  })
}
